package com.msb.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;
import com.msb.mall.product.dao.AttrGroupDao;
import com.msb.mall.product.entity.AttrEntity;
import com.msb.mall.product.entity.AttrGroupEntity;
import com.msb.mall.product.service.AttrGroupService;
import com.msb.mall.product.service.AttrService;
import com.msb.mall.product.vo.AttrGroupWithAttrVo;
import com.msb.mall.product.vo.SpuItemGroupAttrVo;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询列表数据
     *    根据列表编号来查询
     * @param params
     * @param catelogId 如何catelogId为0 就不根据catelogId来查询
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        // key 查询的关键字--搜索查询
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isNullOrEmpty(key)){
            // 拼接查询条件
            queryWrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        if (catelogId == 0){
            // 不根据catelogId查询
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }
        // 根据类别编号查询
        queryWrapper.eq("catelog_Id",catelogId);
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);//
    }

    /**
     * 获取属性组及关联的类别信息
     * @param catalogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrVo> getWithAttr(Long catalogId) {
        List<AttrGroupEntity> groupList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catalogId));
        List<AttrGroupWithAttrVo> list = groupList.stream().map(group -> {
            AttrGroupWithAttrVo vo = new AttrGroupWithAttrVo();
            BeanUtils.copyProperties(group,vo);
            // 从关联表查询出属性id
            List<AttrEntity> attrList = attrService.getAttrRelation(group.getAttrGroupId());

            vo.setAttrs(attrList);
            return vo;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 查询当前商品的规格与参数
     * @param catalogId
     * @param spuId
     * @return
     */
    @Override
    public List<SpuItemGroupAttrVo> queryAttrBycatelogId(Long catalogId, Long spuId) {

        List<SpuItemGroupAttrVo> baseAttr = baseMapper.queryAttrBycatelogId(catalogId,spuId);
        return baseAttr;

    }


}